/*case 1:
tip = "服务器繁忙，请稍后再试。";
break;
case 4:
tip = "设置成功！";
break;
case 5:
tip = "数据拉取失败";
break;
case 6:
tip = "正在加载中，请稍后...";
break;*/

$(function(){
　　var time = $.session.get('btndisptime');
  if(time >= 1){
    distime(time);
  }
});

  $('#send_code').on('click', function () {
    var email = $('#email').val();
    var password = $('#password').val();
    var emailcode = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var pascode = /^[a-zA-z]\w{3,15}$/;

    if(email == ""){
    ZENG.msgbox.show("请输入您的邮箱地址！", 5, 3000);
    }else if(!emailcode.test(email)){
      ZENG.msgbox.show("邮箱地址格式有误！", 5, 3000);
    }else{
      $('#senderror').html('');
      $("#send_code").attr("disabled", "disabled");
      distime(100);
      send_code(email);
    }
  })

function btndisp(){
  var btndisptime = $.session.get('btndisptime');
  if(btndisptime >= 1){
    $("#send_code").attr("disabled", "disabled");
    $("#send_code").html('发送验证码('+btndisptime+')');
  }else{
    $("#send_code").attr("disabled", false);
    // $("#send_code").html('发送验证码');
    $("#send_code").html(btndisptime);
  }
}

function distime(time) {
  $.session.set('btndisptime', time);
  btndisp();
  if(time >= 1){
    time--;
    setTimeout("distime("+time+")",1000);
  }else{
    $("#send_code").attr("disabled", false);
    $("#send_code").html('发送验证码');
  }
}

function send_code(email) {
  $.ajax({
    type: "POST",
    url: "send_code",
    data: {"toaddress":email},
    success: function(msg){
      if(msg == "1" || msg == 1){
          $('#senderror').css('color','#1ABC9C');
          $('#senderror').html('验证码发送成功!');
      }else if(msg == "0" || msg == 0){
          $('#senderror').css('color','#E74C3C');
          $('#senderror').html('验证码发送失败!');
          $("#send_code").attr("disabled", false);
          // $("#send_code").html('发送验证码');
          $("#send_code").html('2');
      }else if(msg == "2" || msg == 2){
          ZENG.msgbox.show("用户不存在，请核对后再试！", 5, 3000);
      }
    }
  });
}

  $('#loginbtn').on('click', function () {
    var email = $('#email').val();
    var password = $('#password').val();
    var code = $('#code').val();
    var emailcode = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var pascode = /^[a-zA-z]\w{3,15}$/;

    if(email == ""){
    ZENG.msgbox.show("请输入您的邮箱地址！", 5, 3000);
    }else if(!emailcode.test(email)){
      ZENG.msgbox.show("邮箱地址格式有误！", 5, 3000);
    }else if(!pascode.test(password) && password.length < 6 || password.length > 16){
      ZENG.msgbox.show("密码错误，请重新输入！", 5, 3000);
    }else if(code.length != 6){
      ZENG.msgbox.show("验证码错误，请重新输入！", 5, 3000);
    }else{
      $.ajax({
        type: "POST",
        url: "login",
        data: {"code":code,"password":password,"email":email},
        success: function(msg){
          if(msg == "0" || msg == 0){
            ZENG.msgbox.show("验证码错误，请重新输入！", 5, 3000);
          }else if(msg == "-1" || msg == -1){
            ZENG.msgbox.show("帐号或密码错误，请重新输入！", 5, 3000);
          }else if(msg == "1" || msg == 1){
            $.session.set('btndisptime', null);
            $.session.remove('btndisptime');
            $('#loginbtn').html('登陆中...');
            location.href = "main";
          }
        }
      });
    }
  })
